#ifndef turret_H
#define turret_H

#include "../sprite_handler.h"
#include "../input.h"
#include "../scrolling.h"
#include "player.h"
#include "../velocity.h"


////////////////////////////////////////////////////////////
// other defines
////////////////////////////////////////////////////////////
//#define ORTHO_VELOCITY 8
//#define DIAG_VELOCITY 11
//#define DIRECTION_CHANGE_INTERVAL 300
//#define SPRITE_SIZE SD_16x16
//#define CENTER_X 112    //(SCREEN_WIDTH >> 1) - 16
//#define CENTER_Y 72     // (SCREEN_HEIGHT >> 1) - 16


////////////////////////////////////////////////////////////
// global variables
////////////////////////////////////////////////////////////

// turret Data
typedef struct STRUCT_turret_DATA
{	
	int map_position_x, map_position_y;
	int ortho_wait_before_move;
	int dir_x, dir_y;
	int wait_x, wait_y;
	int milliseconds_since_last_move_x, milliseconds_since_last_move_y;
	
} TurretData;


////////////////////////////////////////////////////////////
// function prototypes
////////////////////////////////////////////////////////////
void turretUpdateFunction( SpriteHandler * );
void MoveTurret(SpriteHandler *);
void SetTurretAngle(SpriteHandler *);
void SetTurretDirX(SpriteHandler *);
void SetTurretDirY(SpriteHandler *);


void newTurret( int x, int y )
{
    // create new sprite handler for turret
	SpriteHandler * turret = newSpriteHandler();
	
	turret->characterType = CHAR_ENEMY_TURRENT;
	
	// TODO: Set Animations
	
	// Set userdata
	TurretData * turret_data = malloc( sizeof( TurretData ) );	
	memset( turret_data, 0, sizeof( TurretData ) ); // Zero set the userdata
	
	turret->userData = turret_data; // Set the user data
	turret->_imageIndex = FRAME_ENEMY_8x8_1;
	turret->size = SD_8x8;
	turret->x = 0;
	turret->y = 0;
	
	turret_data->map_position_x = x;
	turret_data->map_position_y = y;
	
	turret_data->ortho_wait_before_move = 30;
	
	turret_data->wait_x = 999;
	turret_data->wait_y = 10;
	
	turret_data->dir_x = 0;
	turret_data->dir_y = -1;
	
	turret_data->milliseconds_since_last_move_x = 0;
	turret_data->milliseconds_since_last_move_y = 0;
	
	updateSpriteAttributes( turret );
}


// turret Update Function

void turretUpdateFunction( SpriteHandler * sprite_handler )
{
    TurretData * turret_data = sprite_handler->userData;
    
	if ( sprite_handler->userData == NULL ) {
		return;
	}
	
	SetTurretAngle(sprite_handler);
	SetTurretDirX(sprite_handler);
	SetTurretDirY(sprite_handler);
	
	turret_data->wait_x = GetTimeToWaitBeforeMoveX(turret_data->ortho_wait_before_move, sprite_handler->angle);
    turret_data->wait_y = GetTimeToWaitBeforeMoveY(turret_data->ortho_wait_before_move, sprite_handler->angle);
    
	MoveTurret(sprite_handler);
	
	turret_data->milliseconds_since_last_move_x += getElapsedTime();
    turret_data->milliseconds_since_last_move_y += getElapsedTime();
	
	// Update the Sprite Attributes to reflect the changes we make in sprite_handler
	updateSpriteAttributes( sprite_handler );
}


void MoveTurret(SpriteHandler * sprite_handler)
{
    // TODO: don't move if overlapping player
    
    // TODO: bounds checking
    
    TurretData * turret_data = sprite_handler->userData;
    
    if (turret_data->milliseconds_since_last_move_x > turret_data->wait_x) {
        turret_data->milliseconds_since_last_move_x = 0;
        
        turret_data->map_position_x = turret_data->map_position_x + turret_data->dir_x;
    }
    
    if (turret_data->milliseconds_since_last_move_y > turret_data->wait_y) {
        turret_data->milliseconds_since_last_move_y = 0;

        turret_data->map_position_y = turret_data->map_position_y + turret_data->dir_y;
    }
}


void turretHandleCollision( SpriteHandler * self, SpriteHandler * other )
{
	// self->alive = FALSE;
	
	// updateSpriteAttributes( self );
}


////////////////////////////////////////////////////////////
// Function: SetTurretAngle
// ...
////////////////////////////////////////////////////////////
void SetTurretAngle(SpriteHandler * sprite_handler)
{
    TurretData * turret_data = sprite_handler->userData;
    
    int x_diff =  GetPlayerMapPosX() - turret_data->map_position_x;
    int y_diff =  GetPlayerMapPosY() - turret_data->map_position_y;
    int angle = 0;
    int abs_x_diff = x_diff;
    int abs_y_diff = y_diff;
    
    if (abs_x_diff < 0) {
        abs_x_diff *= -1;
    }
    if (abs_y_diff < 0) {
        abs_y_diff *= -1;
    }
    
    if (x_diff == 0) {
        if (y_diff > 0) {
            angle = 180;
        }
        else if (y_diff < 0) {
            angle = 0;
        }
    }
    else if (x_diff > 0) {
        if (y_diff < 0) {
            if (abs_x_diff <= abs_y_diff) {
                angle = 30;
            }
            else if (abs_x_diff > abs_y_diff) {
                angle = 60;
            }
        }
        else if (y_diff == 0) {
            angle = 90;
        }
        else if (y_diff > 0) {
            if (abs_x_diff >= abs_y_diff) {
                angle = 120;
            }
            else if (abs_x_diff < abs_y_diff) {
                angle = 150;
            }
        }
    }
    else if (x_diff < 0) {
        if (y_diff > 0) {
            if (abs_x_diff <= abs_y_diff) {
                angle = 210;
            }
            else if (abs_x_diff > abs_y_diff) {
                angle = 240;
            }
        }
        else if (y_diff == 0) {
            angle = 270;
        }
        else if (y_diff > 0) {
            if (abs_x_diff >= abs_y_diff) {
                angle = 300;
            }
            else if (abs_x_diff < abs_y_diff) {
                angle = 330;
            }
        }
    }
    
    sprite_handler->angle = angle;
}


////////////////////////////////////////////////////////////
// Function: SetTurretDirX
// ...
////////////////////////////////////////////////////////////
void SetTurretDirX(SpriteHandler * sprite_handler)
{
    int angle = sprite_handler->angle;
    int dir_x = 0;

    if (angle == 0 || angle == 180) {
        dir_x = 0;
    }
    else if (angle > 0 && angle < 180) {
        dir_x = 1;
    }
    else if (angle > 180 && angle < 360) {
        dir_x = -1;
    }

    TurretData * turret_data = sprite_handler->userData;
    turret_data->dir_x = dir_x;
}


////////////////////////////////////////////////////////////
// Function: SetTurretDirY
// ...
////////////////////////////////////////////////////////////
void SetTurretDirY(SpriteHandler * sprite_handler)
{
    int angle = sprite_handler->angle;
    int dir_y = 0;

    if (angle == 90 || angle == 270) {
        dir_y = 0;
    }
    else if ((angle > 270 && angle < 360) || (angle > 0 && angle < 90) || angle == 0) {
        dir_y = -1;
    }
    else if (angle > 90 && angle < 270) {
        dir_y = 1;
    }

    TurretData * turret_data = sprite_handler->userData;
    turret_data->dir_y = dir_y;
}

#endif
